﻿/*
Commerce by Onex – eCommerce software platform (www.ecommerce.byonex.com)
Copyright (C) 2008-2010 by Onex Group

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ShopBLL;
using ShopModel;

public partial class OrderResult : BasePage
{
    #region METHODS

    private void OnlinePayment(Realization order)
    {
        if (Request.QueryString["error"] != null)
        {
            pnlFaul.Visible = true;
            switch (int.Parse(Request.QueryString["error"]))
            {
                case 112:
                    lblFaul.Text = Resources.Language.PlatnosciError112;
                    break;

                case 203:
                    lblFaul.Text = Resources.Language.PlatnosciError203;
                    break;

                case 200:
                    lblFaul.Text = Resources.Language.PlatnosciError200;
                    break;

                case 204:
                    lblFaul.Text = Resources.Language.PlatnosciError204;
                    break;

                case 205:
                    lblFaul.Text = Resources.Language.PlatnosciError205;
                    break;

                case 206:
                    lblFaul.Text = Resources.Language.PlatnosciError206;
                    break;

                case 207:
                    lblFaul.Text = Resources.Language.PlatnosciError207;
                    break;

                case 500:
                    lblFaul.Text = Resources.Language.PlatnosciError500;
                    break;

                case 501:
                    lblFaul.Text = Resources.Language.PlatnosciError501;
                    break;

                case 502:
                    lblFaul.Text = Resources.Language.PlatnosciError502;
                    break;

                case 503:
                    lblFaul.Text = Resources.Language.PlatnosciError503;
                    break;

                case 504:
                    lblFaul.Text = Resources.Language.PlatnosciError504;
                    break;

                case 505:
                    lblFaul.Text = Resources.Language.PlatnosciError505;
                    break;

                case 506:
                    lblFaul.Text = Resources.Language.PlatnosciError506;
                    break;

                case 509:
                    lblFaul.Text = Resources.Language.PlatnosciError509;
                    break;

                default:
                    lblFaul.Text = Resources.Language.ErrorNumber + ": " + Request.QueryString["error"];
                    break;
            }
        }
        else
        {
            pnlOnlinePayments.Visible = true;
            lblOnlineOrderId.Text = order.IDRealization.ToString();
            Dictionary<int, BasketItem> basket = (Dictionary<int, BasketItem>)Session["BasketItemsCopy"];
            ReportsManager.Instance.SendOrderConfirmation(order);
            CheckAmountOfOrderedProducts();
            BasketManager.Instance.ClearBasket(Session.SessionID);
            Session["BasketItemsCopy"] = null;
        }
    }

    private void InstalmentPayment(Realization order)
    {
        if (Request.QueryString["zagiel"] != null)
        {
            if (Request.QueryString["zagiel"] == "0")
            {
                pnlRateCancel.Visible = true;
            }
            else
            {
                pnlRateOk.Visible = true;
                CheckAmountOfOrderedProducts();
                BasketManager.Instance.ClearBasket(Session.SessionID);
                Session["BasketItemsCopy"] = null;
            }
        }
        else
        {
            pnlFaul.Visible = true;
            lblFaul.Text = Resources.Language.OrderResultError + ":<a href=\"mailto:" + ShopModel.Settings.CompanyEmail + "\">" + ShopModel.Settings.CompanyEmail + "</a>";
        }
    }

    private void PayPalPayment(Realization order)
    {
        if (Request.QueryString["paypal"] != null)
        {
            if (Request.QueryString["paypal"] == "0")
            {
                pnlFaul.Visible = true;
            }
            else
            {
                CheckAmountOfOrderedProducts();
                pnlOnlinePayments.Visible = true;
                lblOnlineOrderId.Text = order.IDRealization.ToString();
                Dictionary<int, BasketItem> basket = (Dictionary<int, BasketItem>)Session["BasketItemsCopy"];
                ReportsManager.Instance.SendOrderConfirmation(order);

                BasketManager.Instance.ClearBasket(Session.SessionID);
                Session["BasketItemsCopy"] = null;
            }
        }
    }

    private void CheckAmountOfOrderedProducts()
    {
        if (Session["BasketItemsCopy"] != null)
        {
            bool smallAmount = false;
            Dictionary<int, BasketItem> basketItems = (Dictionary<int, BasketItem>)Session["BasketItemsCopy"];
            foreach (KeyValuePair<int, BasketItem> e in basketItems)
            {
                Products p = ProductsManager.Instance.GetProduct(e.Key, ShopBLL.CommonTools.CurrentLanguageID);
                if (p.AvailableCount < e.Value.Count) // jeśli ilość produktu w bazie będzie mniejsza niż ilość zamówionego produktu
                {
                    smallAmount = true;
                    break;
                }
            }
            if (smallAmount)
            {
                pnlSmallProductsAmount.Visible = true;
            }
        }
    }

    #endregion METHODS

    #region PAGE EVENTS

    protected void Page_PreInit(object sender, EventArgs e)
    {
        if (Session["OrderData"] != null)
        {
            Session["BasketItemsCopy"] = Session["BasketItems"];
            Session.Remove("BasketItems");
        }

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["OrderData"] != null)
        {
            Realization order = (Realization)Session["OrderData"];
            order.OrderedProducts = new List<ProductBatch>();
            Dictionary<int, BasketItem> basket = (Dictionary<int, BasketItem>)Session["BasketItemsCopy"];

            //foreach (int k in basket.Keys)
            //{
            order.OrderedProducts = OrderManager.Instance.GetRealizationOrderedProductsList(order.IDRealization, CommonTools.CurrentLanguageID);//.Add(new ProductBatch(basket[k].ProductId, basket[k].Name, 0, basket[k].PriceBrutto, 0, 0, basket[k].Count, basket[k].VatRateValue));
            //}

            switch (order.Payment.PaymentGroup)
            {
                case PaymentGroups.Delivery:
                    pnlDelivery.Visible = true;
                    lblDeliveryOrderID.Text = order.IDRealization.ToString();
                    lblDeliveryCost.Text = String.Format("{0:F02}", (order.TotalAmountBrutto + order.ShipmentCost));
                    BasketManager.Instance.ClearBasket(Session.SessionID);
                    Session["BasketItemsCopy"] = null;
                    CheckAmountOfOrderedProducts();
                    ReportsManager.Instance.SendOrderConfirmation(order);
                    break;

                case PaymentGroups.Transfer:
                    pnlTransfer.Visible = true;
                    lblTransferTitle.Text = order.IDRealization.ToString();
                    lblTransferCost.Text = String.Format("{0:F02}", (order.TotalAmountBrutto + order.ShipmentCost));
                    BasketManager.Instance.ClearBasket(Session.SessionID);
                    Session["BasketItemsCopy"] = null;
                    CheckAmountOfOrderedProducts();
                    ReportsManager.Instance.SendOrderConfirmation(order);
                    break;

                case PaymentGroups.Online:
                    OnlinePayment(order);
                    break;

                case PaymentGroups.Instalment:
                    InstalmentPayment(order);
                    break;

                case PaymentGroups.PayPal:                    
                    PayPalPayment(order);
                    break;
            }
            Session.Remove("OrderData");
        }
    }

    #endregion PAGE EVENTS
}
